#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@desc: .

@Project ：kuberCloudTest 
@File ：basic.py
@Author ：admin
@Date ：2024/3/20 16:49 
"""
from kuberCloudTest.cloud_resources.vms.urls import VMUrls
from kuberCloudTest.cloud_resources.vms.utils import vmUtils
from kuberCloudTest.common.constant import OFFSET, LIMIT
from kuberCloudTest.common.utils import _getDetail


def getComputeSpec(accessToken, **kwargs):
    """
    获取可用的计算规格
    :param accessToken: access token
    :param kwargs: offset:
                   limit:
                   keyword: xxx
    :return:
    """
    params = {'state': 1,
              'offset': kwargs.get('offset', OFFSET),
              'limit': kwargs.get('limit', LIMIT),
              'keyword': kwargs.get('keyword', '')}
    vm_utils = vmUtils(accessToken=accessToken)
    return vm_utils.cloud_http_request.get(VMUrls.getComputeSpec.value, params=params)


def getDiskSpec(accessToken, **kwargs):
    """
    获取可用的云盘规格
    :param accessToken: access token
    :param kwargs: offset:
                   limit:
                   keyword: xxx
    :return:
    """
    params = {'state': 1,
              'offset': kwargs.get('offset', OFFSET),
              'limit': kwargs.get('limit', LIMIT),
              'keyword': kwargs.get('keyword', '')}
    vm_utils = vmUtils(accessToken=accessToken)
    return vm_utils.cloud_http_request.get(VMUrls.getDiskSpec.value, params)


def getThreeLayerNetwork(accessToken, networkIds=None, networkType: int = 0, **kwargs):
    """
    创建云主机时，查询可用的三层网络
    :param accessToken: access token
    :param networkIds: 三层网络网络UUID，多个UUID由英文逗号隔开（用于过滤网络）
    :param networkType: 网络类型
                        0：全部；1：公有网络；2：私有网络；4：vpc网络
    :param kwargs: limit:
                   offset:
                   keyword: xxx
    :return:
    """
    return vmUtils(accessToken=accessToken).cloud_http_request.get(VMUrls.getThreeLayerNetwork.value,
                                                                   {
                                                                       'offset': kwargs.get('offset', OFFSET),
                                                                       'limit': kwargs.get('limit', LIMIT),
                                                                       'keyword': kwargs.get('keyword', ''),
                                                                       'networkType': networkType,
                                                                       'networkids': networkIds
                                                                   })


def getMachine(accessToken, **kwargs):
    """
    获取可用的物理机
    :param accessToken: access token
    :param kwargs: offset:
                   limit:
                   keyword: xxx
                   networkIds: 网络uuid (多个使用英文逗号分割)
                   msid: 系统盘归属主存储UUID
                   dataMsid:数据盘归属主存储UUID
                   architecture: CPU架构
                   cpuShared: cpu是否共享
    :return:
    """
    params = {'offset': kwargs.get('offset', OFFSET),
              'limit': kwargs.get('limit', LIMIT),
              'keyword': kwargs.get('keyword', ''),
              'networkids': kwargs.get('networkIds', ''),
              'msid': kwargs.get('msid', ''),
              'datamsid': kwargs.get('dataMsid', ''),
              'cpushared': kwargs.get('cpuShared', ''),
              'architecture': kwargs.get('architecture', '')}
    vm_utils = vmUtils(accessToken=accessToken)
    return vm_utils.cloud_http_request.get(VMUrls.getMachine.value, params)


def getMainStorage(accessToken, **kwargs):
    """
    获取可用主存储
    :param accessToken: access token
    :param kwargs: offset:
                   limit:
                   keyword: xxx
                   networkIds: 网络uuid (多个使用英文逗号分割)
                   msid: 系统盘归属主存储UUID
                   dataMsid:数据盘归属主存储UUID
                   hostId: 物理机UUID
                   mirrorFormat: 镜像格式
    :return:
    """
    params = {'offset': kwargs.get('offset', OFFSET),
              'limit': kwargs.get('limit', LIMIT),
              'networkids': kwargs.get('networkIds', ''),
              'msid': kwargs.get('msid', ''),
              'datamsid': kwargs.get('dataMsid', ''),
              'hostId': kwargs.get('hostId', ''),
              'mformat': kwargs.get('mirrorFormat', '')}
    vm_utils = vmUtils(accessToken=accessToken)
    return vm_utils.cloud_http_request.get(VMUrls.getStorage.value, params)


def getMirror(accessToken, **kwargs):
    """
    获取可用的系统镜像
    :param accessToken: access token
    :param kwargs: offset:
                   limit:
                   keyword: xxx
    :return:
    """
    params = {'offset': kwargs.get('offset', OFFSET),
              'limit': kwargs.get('limit', LIMIT),
              'mtype': 1,  # 1: system mirror; 2: disk mirror
              'status': 1,
              'keyword': kwargs.get('keyword', '')}
    vm_utils = vmUtils(accessToken=accessToken)
    return vm_utils.cloud_http_request.get(VMUrls.getMirror.value, params)


def getCDRom(accessToken, **kwargs):
    """
    get eligible cdrom
    :param accessToken: access token
    :param kwargs: offset:
                   limit:
                   keyword: xxx
                   mirrorIds: 镜像UUID(多个使用英文逗号分割)
                   mirrorId: 镜像UUID
                   mirrorFormat: mirror format ('iso' is the only value)
                   excludeMirrorFormat: exclude mirror format (raw or qcow2)
                   rootDiskMsType: 系统盘归属主存储UUID
                   architecture: 镜像架构
                   vmid: 云主机UUID
    :return:
    """
    params = {'mtype': 1,  # 1: system mirror; 2: disk mirror
              'status': 1,
              'offset': kwargs.get('offset', OFFSET),
              'limit': kwargs.get('limit', LIMIT),
              'keyword': kwargs.get('keyword', ''),
              'mirrorids': kwargs['mirrorIds'] if 'mirrorIds' in kwargs.keys() else '',
              'mirrorid': kwargs['mirrorId'] if 'mirrorId' in kwargs.keys() else '',
              'mformat': kwargs['mirrorFormat'] if 'mirrorFormat' in kwargs.keys() else '',
              'notmformat': kwargs['excludeMirrorFormat'] if 'excludeMirrorFormat' in kwargs.keys() else '',
              'rootVolumeMsType': kwargs['rootDiskMsType'] if 'rootDiskMsType' in kwargs.keys() else '',
              'architecture': kwargs['architecture'] if 'architecture' in kwargs.keys() else '',
              'vmid': kwargs['vmid'] if 'vmid' in kwargs.keys() else ''}
    vm_utils = vmUtils(accessToken=accessToken)
    return vm_utils.cloud_http_request.get(VMUrls.getCdRom.value, params)


def getMountableIso(accessToken, vmId, mirrorId=None, **kwargs):
    """
    查找可挂载至光驱的iso文件列表
    :param accessToken:
    :param vmId: 云主机UUID
    :param mirrorId: 镜像ID
    :param kwargs: limit:
                   offset:
                   keyword: xxx
    :return:
    """
    params = {
        'vmid': vmId,
        'status': 1,
        'mformat': 'iso',
        'mirrorid': mirrorId if mirrorId else '',
        'offset': kwargs.get('offset', OFFSET),
        'limit': kwargs.get('limit', LIMIT),
        'keyword': kwargs.get('keyword', '')
    }
    vm_utils = vmUtils(accessToken=accessToken)
    return vm_utils.cloud_http_request.get(VMUrls.getMountableIso.value, params)


def getLabel(accessToken, **kwargs):
    """
    获取标签
    :param accessToken: access token
    :param kwargs: offset:
                   limit:
    :return:
    """
    params = {'offset': kwargs.get('offset', OFFSET),
              'limit': kwargs.get('limit', LIMIT),
              'order': 'asc',
              'searchitem': 0}
    vm_utils = vmUtils(accessToken=accessToken)
    return vm_utils.cloud_http_request.get(VMUrls.getLabel.value, params)


def getUnbindLabel(accessToken, vmid, **kwargs):
    """
    获取云主机未绑定的标签
    :param accessToken: access token
    :param vmid: 云主机UUID
    :param kwargs: offset:
                   limit:
    :return:
    """
    params = {'offset': kwargs.get('offset', OFFSET),
              'limit': kwargs.get('limit', LIMIT),
              'order': 'asc',
              'vmid': vmid}
    vm_utils = vmUtils(accessToken=accessToken)
    return vm_utils.cloud_http_request.get(VMUrls.getUnbindLabel.value, params)


def getBindLabel(accessToken, vmid, **kwargs):
    """
    获取云主机已绑定的标签
    :param accessToken: access token
    :param vmid: 云主机UUID
    :param kwargs: offset:
                   limit:
    :return:
    """
    params = {'offset': kwargs.get('offset', OFFSET),
              'limit': kwargs.get('limit', LIMIT),
              'order': 'asc',
              'vmid': vmid}
    vm_utils = vmUtils(accessToken=accessToken)
    return vm_utils.cloud_http_request.get(VMUrls.getBindLabel.value, params)


def getNetwork(accessToken, networkId=None, **kwargs):
    """
    获取可用三层网络
    :param accessToken: access token
    :param networkId:
    :param kwargs: offset:
                   limit:
                   keyword: xxx
                   msClusterId: 系统盘归属主存储的集群UUID
                   dataMsClusterId: 数据盘归属主存储的集群UUID
                   networkIds: 三层网络UUID(多个使用英文逗号分割)
                   networkId: 三层网络UUID
                   networkType: 三层网络类型
                        0: all
                        1: 公有网络
                        2: 私有网络
                        3: vpc网络
    :return:
    """
    params = {'offset': kwargs.get('offset', OFFSET),
              'limit': kwargs.get('limit', LIMIT),
              'keyword': kwargs.get('keyword', ''),
              'msClusterId': kwargs.get('msClusterId', ''),
              'dataMsClusterId': kwargs.get('dataMsClusterId', ''),
              'hostClusterId': kwargs.get('hostClusterId', ''),
              'networkids': kwargs.get('networkIds', ''),
              'networkId': kwargs.get('networkId', ''),
              'networkType': kwargs.get('networkType', 0)}
    vm_utils = vmUtils(accessToken=accessToken)
    content = vm_utils.cloud_http_request.get(VMUrls.getNetwork.value, params)
    if networkId:
        total = content.get('total', 0)
        content = vm_utils.cloud_http_request.get(VMUrls.getNetwork.value, {'limit': total, 'offset': OFFSET})
        content = _getDetail(content, networkId, 'nid')
    return content


def getHugePage(accessToken, hostId, **kwargs):
    """
    获取可用内存大页
    :param accessToken: access token
    :param hostId: 物理机UUID
    :param kwargs: offset:
                   limit:
    :return:
    """
    params = {'offset': kwargs.get('offset', OFFSET),
              'limit': kwargs.get('limit', LIMIT),
              'hostid': hostId}
    vm_utils = vmUtils(accessToken=accessToken)
    return vm_utils.cloud_http_request.get(VMUrls.getHugePage.value, params)
